using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Forum.Contracts;

namespace Forum.Client
{
    public partial class Form1 : Form
    {
        private IApplication app;

        private List<Posting> postings;


        public Form1()
        {
            InitializeComponent();

            this.app = new Forum.Application.ApplicationLogic();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.postings = new List<Posting>(this.app.GetAllPostings());
            DisplayPostings(postings);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.app.Dispose();
        }


        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count > 0 && e.Node.Nodes[0].Tag == null)
            {
                e.Node.Nodes.Clear();
                DisplayPostings(e.Node, ((Posting)e.Node.Tag).Replies);
            }
        }


        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.textBox1.Text = ((Posting)e.Node.Tag).Text;
        }


        private void newPostingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Posting p = this.app.CreatePosting();
            p.Subject = "s" + DateTime.Now.ToString();
            p.Text = "t" + DateTime.Now.ToString();
            p.CreatedBy = "c" + DateTime.Now.Second;
            this.app.StorePosting(p);

            this.postings.Add(p);

            this.treeView1.SelectedNode = DisplayNode(null, p);
        }


        private void replyToPostingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                if (!this.treeView1.SelectedNode.IsExpanded)
                    this.treeView1.SelectedNode.Expand();
                Posting parent = (Posting)this.treeView1.SelectedNode.Tag;
                
                Posting p = this.app.CreateReply(parent);
                p.Subject = "re:s" + DateTime.Now.ToString();
                p.Text = "t" + DateTime.Now.ToString();
                p.CreatedBy = "c" + DateTime.Now.Second;
                this.app.StorePosting(p);

                parent.Replies.Add(p);

                TreeNode np = DisplayNode(this.treeView1.SelectedNode, p);
                np.EnsureVisible();
                this.treeView1.SelectedNode = np;
            }
        }
        
        
        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.app.Clear();
            this.Close();
        }
        
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void DisplayPostings(IEnumerable<Posting> postings)
        {
            this.treeView1.Nodes.Clear();
            foreach (Posting p in postings)
                DisplayNode(null, p);
        }

        private void DisplayPostings(TreeNode parentNode, IEnumerable<Posting> postings)
        {
            foreach (Posting p in postings)
                DisplayNode(parentNode, p);
        }

        private TreeNode DisplayNode(TreeNode parentNode, Posting p)
        {
            TreeNode np;

            string label = string.Format("{0} [{1} @ {2}]", p.Subject, p.CreatedBy, p.CreatedAt);
            if (parentNode == null)
                np = this.treeView1.Nodes.Add(label);
            else
                np = parentNode.Nodes.Add(label);
            np.Tag = p;

            np.Nodes.Add("to be replaced by replies upon expansion of parent");
            
            return np;
        }
    }
}